<template>
  <div class="panel">
    <el-dialog title="编辑权限" @close="cancel" :visible="showDialog">
      <el-form :model="form" label-width="100px">
        <el-form-item label="权限名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="form.code"></el-input>
        </el-form-item>

        <el-form-item label="权限描述">
          <el-input v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item label="企业可见">
          <el-switch
            v-model="form.enVisible"
            active-color="#409eff"
            inactive-color="#dcdfe6"
            active-value="1"
            inactive-value="0"
          >
          </el-switch>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getPermissionByIdApi,
  editPermissionByIdApi,
  addPermissionApi
} from '@/api/permission'
export default {
  props: ['showDialog'],
  data () {
    return {
      form: {
        name: '',
        code: '',
        description: '',
        enVisible: ''
      }
    }
  },
  methods: {
    cancel () {
      this.$emit('update:showDialog', false)
      this.form = {
        name: '',
        code: '',
        description: '',
        enVisible: ''
      }
    },
    async submit () {
      try {
        if (this.form.id) {
          // 编辑
          await editPermissionByIdApi(this.form)
        } else {
          // 新增
          await addPermissionApi(this.form)
          console.log(this.form);
        }

        this.$message.success('新增成功')
        this.$emit('update:showDialog', false)
        this.form = {
          name: '',
          code: '',
          description: '',
          enVisible: ''
        }
        this.$parent.getAllExpression()
      } catch (error) {
        console.log(error)
      }
    },
    // 获取所有权限
    async getAllPermission (id) {
      const res = await getPermissionByIdApi(id)
      // console.log(res)
      this.form = res.data
    },
    setProp(type,pid) {
      this.form.type=type
      this.form.pid = pid
    }
  }
}
</script>

<style lang="scss" scoped>
.panel {
}
</style>
